package cn.mygweb.common.utils;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Hashtable;

import javax.imageio.ImageIO;

import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;


/** 
* <p>Title: QrcodeUtil.java</p>
* <p>Description: 二维码生成-解析工具类</p>  
* @author gxb 
* @date 2019年7月24日 
* <p>Copyright: Copyright (c) 2019</p>
* <p>Company: www.mygweb.cn</p> 
*/
public class QrcodeUtil {

	/*
	 * rgba形式表示颜色，前面的FF表示透明度
	 */
	private static final int BLANK = 0xFF000000;
	private static final int WHITE = 0xFFFFFFFF;
	
	public static BufferedImage toBufferedImage(BitMatrix matrix) {
		int width = matrix.getWidth();
		int height = matrix.getHeight();
		
		//TYPE_INT_RGB表示一个图像,它具有合成整数像素的8位rgb颜色分量
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		for(int x = 0; x < width; x++) {
			for(int y = 0; y < height; y++) {
				image.setRGB(x, y, matrix.get(x, y) ? BLANK : WHITE);
			}
		}
		return image;
	}
	
	/**
	 * <p>Title: writeToFile</p>
	 * <p>Description: 使用支持给定格式的任意ImageWriter将一个图像写入file，
	 * 	如果已有一个file存在，则丢弃其内容</p>
	 * @param matrix
	 * @param format
	 * @param file
	 * @throws IOException 
	 */
	private static void writeToFile(BitMatrix matrix, String format, File file) throws IOException {
		BufferedImage image = toBufferedImage(matrix);
		if(!ImageIO.write(image, format, file))
			throw new IOException("无法将“" + format + "”格式的图片写入【" + file + "】文件夹中");
	}
	
	/**
	 * <p>Title: writeStream</p>
	 * <p>Description: 使用支持给定格式的任意ImageWriter将一个图片写入OUtputStream，此方法
	 * 	在写入操作完成后不会关闭提供的OutputStream；如果需要，调用者负责关闭该流</p>
	 * @param matrix
	 * @param format
	 * @param stream
	 * @throws IOException
	 */
	public static void writeStream(BitMatrix matrix, String format, OutputStream stream) throws IOException {
		BufferedImage image = toBufferedImage(matrix);
		if(!ImageIO.write(image, format, stream))
			throw new IOException("无法写入“" + format + "”格式的图片");
	}
	
	/**
	 * <p>Title: decodeQrcode</p>
	 * <p>Description: 将二维码解析为文字</p>
	 * @param filePath
	 * @return
	 */
	public static Result decodeQrcode(String filePath) {
		BufferedImage image = null;
		try {
			image = ImageIO.read(new File(filePath));
		} catch (Exception e) {
			e.printStackTrace();
		}
		LuminanceSource source = new BufferedImageLuminanceSource(image);
		BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
		Hashtable<DecodeHintType, String> hints = new Hashtable<DecodeHintType, String>();
		hints.put(DecodeHintType.CHARACTER_SET, "GBK");
		Result result = null;
		try {
			result = new MultiFormatReader().decode(bitmap, hints);
		} catch (NotFoundException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public static void main(String[] args) {
		Result result = decodeQrcode("D:\\safeqrcode");
		System.out.println(result.getText());
	}
}
